<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: UsergroupController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class System_UsergroupController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'UserGroup' );
		$this->userGroup = new UserGroup ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		$params=$this->getRequest()->getParams();
		$form_url=$params;
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		Baogg_Language::loadFile("level");
		$cm=array();
		$cm['user_group_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['user_group_name']=array("hidden"=>true);
		$cm['user_group_name2']=array("sortable"=>false);
		$cm['user_group_pid']=array("hidden"=>true);
		$cm['user_group_p_name']=array("sortable"=>false);
		//$cm['level_id']=array("hidden"=>true);
		//$cm['level_name']=array("sortable"=>false);
		$cm['state']=array("sortable"=>true);

		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));
		
		//$this->view->form_url=BAOGG_BASE_URL."system/usergroup/save";
		
		$rs_userGroup=$this->userGroup->getList(array("state"=>1),NULL,'',array("user_group_id","user_group_name","user_group_pid"));
		
		$store_user_group_pid=array(array("0",BAOGG_LANGUAGE::get('please_select')));
		$objTree	=new Baogg_Db_Tree();
		$tr=$objTree->rs2ComboTree($rs_userGroup);	
		$store_user_group_pid += $objTree->tr2ComboStore($tr,0,0);
		
		$model=new Model();
		$rs_model=$model->getList(array("state"=>1),array("sort"=>"ordering","dir"=>"asc"),NULL,array("model_id","model_name"));
		$arr_model=array();
		foreach((array)$rs_model as $v){
			$arr_model[]=array($v['model_id'],$v['model_name']);
		}
		$store_model_id = $arr_model;
		//echo '<pre>';print_r($arr_model);exit;
		
		$Level=new Level();
		$rs_level=$Level->getList(array("state"=>1),array("sort"=>"ordering","dir"=>"asc"));
		$arr_level=array();
		foreach((array)$rs_level as $v){
			$arr_level[]=array($v['level_id'],$v['level_name']);
		}
		
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));

		//$form=new Baogg_View_Extform(array("height"=>120));		
		$form->addItem(0,array('name'=>'user_group_name'));
		$form->addItem(0,array('name'=>'user_group_pid','xtype'=>'combo','store'=>$store_user_group_pid));		
		//$form->addItem(1,array('name'=>'level_id','xtype'=>'combo','store'=>$arr_level));
		$form->addItem(0,array('name'=>'ordering'));
		$form->addItem(2,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		
		$form->addItem(2,array('name'=>'user_group_id',"xtype"=>"hidden","hideLabel"=>true));		
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		
		$this->view->form=$form;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		
		$this->userGroup->updatePids();
	    $post=$this->_request->getPost ();
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="user_group_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   
	   
	     $rs_cnt=$this->userGroup->getList($where,$order,NULL,array("count(*) as cnt"));
	     
	   $rs_tr=$this->userGroup->getList($where,NULL,NULL,array("user_group_pids"));
	   $pids="0,";
	   foreach($rs_tr as $v){
	   	 $pids.=$v['user_group_pids'];
	   }
	   $arr_pids=array_filter(array_unique(explode(",",$pids)));
	   $where=array("user_group_id"=>$arr_pids);	   
	   $rs=$this->userGroup->getList($where,$order,NULL,array("user_group_id","user_group_name as user_group_name2","user_group_pid","*"));	   
	
	   $objTree=new Baogg_Db_Tree();
	   $tr=$objTree->rs2GridTree($rs);
	   $store=$objTree->tr2GridStore($tr);
	   
	   $Level=new Level();
	   $rs_level=$Level->getList(array("state"=>1));
	   $arr_level=array();
	   foreach((array)$rs_level as $v){
	   		$arr_level[$v['level_id']]=$v['level_name'];
	   }
	   
	   foreach((array)$store as $k=>$v){
	   		$store[$k]['level_name']=@$arr_level[$v['level_id']];
	   		if($v['user_group_pid']==0){
	   			$store[$k]['user_group_p_name']="";
	   		}else{
	   			$rs_pid=$this->userGroup->getOne($v['user_group_pid']);
	   			$store[$k]['user_group_p_name']=$rs_pid? $rs_pid[0]['user_group_name']:"";
	   		}
	   }
	  // echo '<pre>';print_r($store);exit;
		$this->view->grid = Zend_Json::encode(array("count"=>$rs_cnt[0]['cnt'],"data"=>$store));
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}
	

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->userGroup->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
	}
	
	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
		$arr=$this->userGroup->filterForm($formData);
        $action=$this->getRequest()->getParam('action');
		$rs = $this->userGroup->getSame($arr);
		if($action=='edit'){
			//update record
			//$permission_type = 2;
			//echo '<pre>';print_r($rs);var_dump(count($rs));exit;
			if( !$rs || count($rs)==1 &&  $rs[0] == $arr["user_group_id"] ){
			    $this->userGroup->_update($arr,$arr["user_group_id"]);
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["user_group_id"]);
				$ret['success'] = $this->userGroup->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
		$this->userGroup->updatePids();
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
		
	}

}
